#coding: utf8
import re
from pygeocoder import Geocoder
import time
from datetime import datetime
import urlparse
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview
from decimal import *

class RestaloUtils(object):

    utils = None

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            restaurant['restaurant_name'] = self.__getRestName(self.utils.convertListToString(hxs.select('//div[@class="restaurantHeader clearfix"]/div/strong/text()').extract()))
            self.utils.logProcessDetail("NAME: " + restaurant['restaurant_name'],self.utils.DEBUG)
            subtitle = self.utils.convertListToString(hxs.select('//div[@class="restaurantHeader clearfix"]/div/em/text()').extract())
            self.utils.logProcessDetail("subtitle: " + subtitle,self.utils.DEBUG)
            # check if restaurant has reviews, otherwise return
            hasReviews = False
            nroReviews = 0
            for hr in hxs.select('//ul[@class="mainNav clearfix"]/li'):
                header = str(hr.select('a/text()').extract())
                self.utils.logProcessDetail("header : " + str(hr.select('a/text()').extract()),self.utils.DEBUG)
                if "Opiniones" in header:
                    url_opinions = self.utils.convertListToString(hr.select('a/attribute::href').extract())
                    nroReviews = self.__getNroReviews(header)
                    if int(nroReviews) > 0:
                        hasReviews = True
                        break
            self.utils.logProcessDetail("hasReviews: " + str(hasReviews),self.utils.DEBUG)
            self.utils.logProcessDetail("nroReviews: " + str(nroReviews),self.utils.DEBUG)

            if not hasReviews:
                self.utils.logProcessDetail("RESTO: no hay opiniones" ,self.utils.DEBUG)
                restaurant['score']='NA'
                return

            # Chequeo que no nos encontremos en una url que no sea la de opiniones
            if hasReviews and restaurant['url_scrapy'].find("/slug/") <= 0:
                self.utils.logProcessDetail("RESTO: hay opiniones pero estamos en la web. Se debe cambiar la URL en la base de datos para la próxima vez" ,self.utils.DEBUG)
                restaurant['score']='SA'
                restaurant['url_scrapy'] = url_opinions
                return

            address = subtitle.split("|")
            addressS = address[2].strip() + ", " + address[3].strip()
            self.utils.logProcessDetail("addressS: " + addressS,self.utils.DEBUG) 
            restaurant['city']='NA' 
            restaurant['address'] = addressS
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(addressS)
            self.utils.logProcessDetail("address_google: " + restaurant['address_google'],self.utils.DEBUG)
            restaurant['score']=self.__getScore(self.utils.convertListToString(hxs.select('//div[@id="allReviews"]/div/div[@class="totalScore"]/text()').extract()))
            self.utils.logProcessDetail("score: " + str(restaurant['score']),self.utils.DEBUG)
            restaurant['reviews_total']=self.utils.convertListToString(hxs.select('//div[@class="grid_11 reviewResume"]/div[@class="reviewTabWrapper"]/a[@class="reviewTab active clearfix"]/em/text()').extract())
            restaurant['reviews'] = []


            #list of types of restaurant
            types_rest = list()
            types_rest.append(address[0].strip())
            restaurant['rest_type']=types_rest
            self.utils.logProcessDetail("REST TYPE: " + "".join(restaurant['rest_type']),self.utils.DEBUG)

            sComida=None
            sServicio=None
            sAtmosfera=None
            sCalidad=None
            for r in hxs.select('//div[@class="grid_11 reviewResume"]/div[@id="allReviews"]/div[@class="clearfix"]/div[@class="reviewResume clearfix"]/span'):
                
                
                tmp = r.select('text()').extract()
                tmp_s = self.utils.convertListToString(tmp).strip().lower()
                if tmp_s == "comida":
                    sComida = self.__getScore(self.utils.convertListToString(r.select('em/text()').extract()))
                    continue
                if tmp_s == "servicio":
                    sServicio = self.__getScore(self.utils.convertListToString(r.select('em/text()').extract()))
                    continue
                if tmp_s == "ambiente":
                    sAtmosfera = self.__getScore(self.utils.convertListToString(r.select('em/text()').extract()))
                    continue
                if tmp_s == "calidad/precio":
                    sCalidad = self.__getScore(self.utils.convertListToString(r.select('em/text()').extract()))
                    continue
                
            restaurant['score_comida']=sComida
            restaurant['score_servicio']=sServicio
            restaurant['score_atmosfera']=sAtmosfera
            restaurant['score_calidadP']=sCalidad
            restaurant['url_rest']=None
            restaurant['cp']=None
            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)

    def __getCommentDate(self,inDate):
        try:
            txtDate=inDate[-8:]
            txtDate=txtDate.strip()
            self.utils.log("DEBUG","date: "+txtDate,self.utils.DEBUG)
            dDate = datetime.strptime(txtDate, '%d/%m/%y').date()
            txtDate = dDate.strftime('%d/%m/%Y')
            return txtDate

        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e) + "date: " + str(inDate),self.utils.ERROR)



    def fillComments(self,response,hxs,restaurant):
        try:
            for l in hxs.select('//div[@class="restaurantBlock reviewsBlock grid_11 alpha omega"]/div[@class="singleReview grid_11 alpha omega clearfix"]'):
                restaurantReview = RestaurantReview()
                restaurantReview['author']= self.utils.convertListToString(l.select('div[@class="grid_9 omega"]/span[@class="reviewerDetails"]/strong/text()').extract())
                self.utils.logProcessDetail("author: " + restaurantReview['author'],self.utils.DEBUG)
                self.utils.logProcessDetail("date: " + str(len(self.utils.convertListToString(l.select('div[@class="grid_9 omega"]/span/text()').extract()))),self.utils.DEBUG)
                restaurantReview['score']=self.__getScore(self.utils.convertListToString(l.select('div[@class="grid_2 alpha"]/span/text()').extract()))
                restaurantReview['comment']=self.utils.convertListToString(l.select('div[@class="grid_9 omega"]/p/text()').extract())
                #restaurantReview['score_comida']=self.__getScore(self.utils.convertListToString(l.select('((li[@class="bars fLeft"])[1]/div)[1]/span/em/text()').extract()))
                #restaurantReview['score_servicio']=self.__getScore(self.utils.convertListToString(l.select('((li[@class="bars fLeft"])[1]/div)[2]/span/em/text()').extract()))
                #restaurantReview['score_atmosfera']=self.__getScore(self.utils.convertListToString(l.select('((li[@class="bars fLeft"])[2]/div)[1]/span/em/text()').extract()))
                #restaurantReview['score_calidadP']=self.__getScore(self.utils.convertListToString(l.select('((li[@class="bars fLeft"])[2]/div)[2]/span/em/text()').extract()))             
                if len(self.utils.convertListToString(l.select('div[@class="grid_9 omega"]/span/text()').extract())) > 0:
                    restaurantReview['date']= self.__getCommentDate(self.utils.convertListToString(l.select('div[@class="grid_9 omega"]/span/text()').extract()))
                    restaurant['reviews'].append(restaurantReview)

            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillComments: " + str(e),self.utils.ERROR)

    def __getRestType(self,hxs):
        try:
            d=dict()
            for rest_type in hxs.select('span/text()').extract():
                if rest_type is not None and len(rest_type)>0:
                    sRest_type = self.utils.convertListToString(rest_type)
                    if sRest_type.find("Tipo de Cocina:") == -1:
                        sRest_type = sRest_type.replace("|","")
                        sRest_type = sRest_type.strip()
                        d[sRest_type]=0
            return d.keys()
        

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)


    def __getRestName(self,txtName):
        txtName = txtName.replace("Restaurante","")
        return txtName.strip()
    
    def __getReviews(self,txtReviews):
        tmp_s=""
        try:
            tmp_s = str(txtReviews)
            if len(tmp_s)>0: 
                tmp_s = tmp_s.replace("(","")
                tmp_s = tmp_s.replace(")","")
                tmp_s = tmp_s.replace("de","")
                tmp_s = tmp_s.replace("Opiniones","")
                tmp_s = tmp_s.replace("opiniones","")
                tmp_s = tmp_s.replace("Opinion","")
                tmp_s = tmp_s.replace("opinion","")
                tmp_s = str(int(tmp_s.strip()))
            return tmp_s
        except Exception,e:
            self.utils.logProcessDetail("Error in __getReviews, data_: " + str(txtReviews) + "Error: " + str(e),self.utils.ERROR)
        
    def __getNroReviews(self,txtHeader):
        try:
            indice = txtHeader.index("(")
            return txtHeader[(indice+1):(indice+2)]
        except Exception,e:
            self.utils.logProcessDetail("Error in __getNroReviews data_: " + str(txtHeader) + "Error: " + str(e),self.utils.ERROR)          
    
    def __getScore(self,txtScore):
        tmp_s=""
        try:
            if len(txtScore)>0:
                tmp_s = str(txtScore).replace(",",".")
                tmp_s = str(Decimal(tmp_s))
            return tmp_s
        except Exception,e:
            self.utils.logProcessDetail("Error in __getScore, data_: " + str(txtScore) + "Error: " + str(e),self.utils.ERROR)
           


        